package com.ruoyi.web.controller.system;

import com.mysql.cj.xdevapi.Table;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Category;
import com.ruoyi.system.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping("/save")
    public AjaxResult saveCategory(Category category){
        return toAjax(categoryService.createCategory(category));
    }

    /**
     * 分页获取分类列表
     * @param category
     * @return
     */
    @GetMapping("/categoryList")
    public TableDataInfo listCategory(Category category){
        startPage();
        List<Category> categoryList = categoryService.getCategoryList(category);
        return getDataTable(categoryList);
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PostMapping("/updateCategory")
    public AjaxResult updateCategory(Category category){
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除分类
     * @param category
     * @return
     */
    @PutMapping("/removeCategory")
    public AjaxResult deleteCategory(Category category){
        return toAjax(categoryService.deleteCategory(category));
    }

    /**
     * 获取分类信息
     * @param id
     * @return
     */
    @GetMapping("/getCategory/{id}")
    public AjaxResult getCategoryId(@PathVariable Integer id){
        AjaxResult ajax = AjaxResult.success();
        Category category = categoryService.getCategory(id);
        ajax.put(AjaxResult.DATA_TAG,category);
        return ajax;
    }

    @GetMapping("/search")
    public TableDataInfo searchList(Category category){
        startPage();
        List<Category> categories = categoryService.searchCategory(category);
        return getDataTable(categories);
    }

    @GetMapping("/getCategoryArray")
    public TableDataInfo getCategoryNameArray(String categoryName){
        startPage();
        List<String> categoryNameList = categoryService.getCategoryName(categoryName);
        return getDataTable(categoryNameList);
    }
}
